home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 681 / term / source.lha / termCrypt.c < prev    next >
C/C++ Source or Header  |  1992-05-09  |  6KB  |  294 lines

  1. /*
  2. **    $Id: termCrypt.c,v 1.2 92/04/06 20:11:29 olsen Sta Locker: olsen $
  3. **    $Revision: 1.2 $
  4. **    $Date: 92/04/06 20:11:29 $
  5. **
  6. **    Data encryption routines
  7. **
  8. **    Copyright © 1990-1992 by Olaf `Olsen' Barthel & MXM
  9. **        All Rights Reserved
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* The width of the cell ring is defined here. A word of warning: never
  15.      * encrypt a file and forget the access password, since it will be
  16.      * extremely hard to break the code (with 30 cells there are 256^30
  17.      * possible values each cell may be initialized to which means that
  18.      * you could have to try more than 1.7e+72 combinations for each single
  19.      * password character before actually finding the matching password).
  20.      * See `Random sequence generation by cellular automata' by Steven
  21.      * Wolfram (Institute for Advanced Study; Advances in Applied
  22.      * Mathematics) for more information.
  23.      */
  24.  
  25. #define CELL_WIDTH 30
  26.  
  27.     /* The cell ring and the ring index pointers. */
  28.  
  29. STATIC UBYTE Cell[2][CELL_WIDTH + 2],From,To;
  30.  
  31.     /* The pattern the cell ring will be filled with. The pattern
  32.      * may seem familiar: these are the first 32 digits of Pi.
  33.      */
  34.  
  35. STATIC UBYTE Pi[32] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5 };
  36.  
  37.     /* Automaton():
  38.      *
  39.      *    A cellular automaton working on a ring of celles, producing
  40.      *    random data in each single cell .
  41.      */
  42.  
  43. STATIC UBYTE
  44. Automaton()
  45. {
  46.     register WORD i;
  47.  
  48.         /* Operate on the cell ring... */
  49.  
  50.     for(i = 1 ; i <= CELL_WIDTH ; i++)
  51.         Cell[To][i] = Cell[From][i - 1] ^ (Cell[From][i] | Cell[From][i + 1]);
  52.  
  53.         /* Operate on first and last element. */
  54.  
  55.     Cell[To][0]            = Cell[From][CELL_WIDTH + 1] ^ (Cell[From][0]              | Cell[From][1]);
  56.     Cell[To][CELL_WIDTH + 1]    = Cell[From][CELL_WIDTH]     ^ (Cell[From][CELL_WIDTH + 1] | Cell[From][0]);
  57.  
  58.         /* Swap cell rings. */
  59.  
  60.     To     = From;
  61.     From    ^= 1;
  62.  
  63.         /* Return contents of first cell. */
  64.  
  65.     return(Cell[From][0]);
  66. }
  67.  
  68.     /* Encrypt(UBYTE *Source,UBYTE *Destination,UBYTE *Key):
  69.      *
  70.      *    Encrypt data using cellular automaton as a random number generator.
  71.      */
  72.  
  73. UBYTE *
  74. Encrypt(UBYTE *Source,WORD SourceLen,UBYTE *Destination,UBYTE *Key,WORD KeyLen,BYTE Prefill)
  75. {
  76.     register WORD i,j;
  77.  
  78.         /* Set up cell ring index pointers. */
  79.  
  80.     From    = 0;
  81.     To    = 1;
  82.  
  83.     if(Prefill)
  84.     {
  85.         memcpy(&Cell[0][KeyLen],Pi,32 - KeyLen);
  86.  
  87.         memcpy(Cell[0],Key,KeyLen);
  88.     }
  89.     else
  90.     {
  91.             /* Initialize the cell ring with the key contents. */
  92.  
  93.         for(i = 0 , j = KeyLen - 1 ; i < CELL_WIDTH + 2 ; i++)
  94.         {
  95.             Cell[0][i] = Key[j];
  96.  
  97.             if(j)
  98.                 j--;
  99.             else
  100.                 j = KeyLen - 1;
  101.         }
  102.     }
  103.  
  104.         /* Encrypt the source data. */
  105.  
  106.     for(i = 0 ; i < SourceLen ; i++)
  107.         Destination[i] = ((WORD)Source[i] + Automaton()) % 256;
  108.  
  109.         /* Return result. */
  110.  
  111.     return(Destination);
  112. }
  113.  
  114.     /* Decrypt(UBYTE *Source,UBYTE *Destination,UBYTE *Key):
  115.      *
  116.      *    Decrypt data using cellular automaton as a random number generator.
  117.      */
  118.  
  119. UBYTE *
  120. Decrypt(UBYTE *Source,WORD SourceLen,UBYTE *Destination,UBYTE *Key,WORD KeyLen,BYTE Prefill)
  121. {
  122.     register WORD i,j,Code;
  123.  
  124.         /* Set up cell ring index pointers. */
  125.  
  126.     From    = 0;
  127.     To    = 1;
  128.  
  129.     if(Prefill)
  130.     {
  131.         memcpy(&Cell[0][KeyLen],Pi,32 - KeyLen);
  132.  
  133.         memcpy(Cell[0],Key,KeyLen);
  134.     }
  135.     else
  136.     {
  137.             /* Initialize the cell ring with the key contents. */
  138.  
  139.         for(i = 0 , j = KeyLen - 1 ; i < CELL_WIDTH + 2 ; i++)
  140.         {
  141.             Cell[0][i] = Key[j];
  142.  
  143.             if(j)
  144.                 j--;
  145.             else
  146.                 j = KeyLen - 1;
  147.         }
  148.     }
  149.  
  150.         /* Decrypt the source data. */
  151.  
  152.     for(i = 0 ; i < SourceLen ; i++)
  153.     {
  154.         if((Code = Source[i] - Automaton()) < 0)
  155.             Code = 256 + Code;
  156.  
  157.         Destination[i] = Code;
  158.     }
  159.  
  160.         /* Return result. */
  161.  
  162.     return(Destination);
  163. }
  164.  
  165.     /* CryptPanel(UBYTE *Buffer):
  166.      *
  167.      *    Ask the user to enter an access password, do not display
  168.      *    it while typing.
  169.      */
  170.  
  171. BYTE
  172. CryptPanel(UBYTE *Buffer)
  173. {
  174.     STATIC struct IntuiText IText[] =
  175.     {
  176.         1,0,JAM1,30,19,&DefaultFont,NULL,    &IText[1],
  177.         1,0,JAM1,46,28,&DefaultFont,NULL,    NULL
  178.     };
  179.  
  180.     struct Window    *PanelWindow;
  181.     BYTE         Result = FALSE,FgPen;
  182.  
  183.     IText[0] . IText = LocaleString(MSG_TERMCRYPT_PLEASE_ENTER_ACCESS_PASSWORD_TXT);
  184.     IText[1] . IText = LocaleString(MSG_TERMCRYPT_PRESS_RETURN_WHEN_DONE_TXT);
  185.  
  186.     switch(Config . ColourMode)
  187.     {
  188.         case COLOUR_EIGHT:    FgPen = 7;
  189.                     break;
  190.  
  191.         case COLOUR_SIXTEEN:    FgPen = 15;
  192.                     break;
  193.  
  194.         case COLOUR_AMIGA:    if(Config . DisableBlinking & TERMINAL_FASTER)
  195.                         FgPen = 1;
  196.                     else
  197.                         FgPen = 2;
  198.  
  199.                     break;
  200.  
  201.         case COLOUR_MONO:    FgPen = 1;
  202.  
  203.                     break;
  204.     }
  205.  
  206.     IText[0] . FrontPen = IText[1] . FrontPen = FgPen;
  207.  
  208.     if(PanelWindow = OpenWindowTags(NULL,
  209.         WA_Width,    294,
  210.         WA_Height,    46,
  211.  
  212.         WA_Left,    (Screen -> Width - 294) >> 1,
  213.         WA_Top,        (Screen -> Height - 46) >> 1,
  214.         WA_Activate,    TRUE,
  215.         WA_DragBar,    TRUE,
  216.         WA_DepthGadget,    TRUE,
  217.         WA_CloseGadget,    TRUE,
  218.         WA_RMBTrap,    TRUE,
  219.         WA_CustomScreen,Screen,
  220.  
  221.         WA_IDCMP,    IDCMP_CLOSEWINDOW | IDCMP_VANILLAKEY,
  222.  
  223.         WA_Title,    LocaleString(MSG_TERMCRYPT_ENTER_PASSWORD_TXT),
  224.     TAG_DONE))
  225.     {
  226.         struct IntuiMessage    *Massage;
  227.         ULONG             Class,Code;
  228.         BYTE             Terminated    = FALSE,
  229.                      Count        = 0;
  230.  
  231.         PrintIText(PanelWindow -> RPort,&IText[0],0,0);
  232.  
  233.         DrawBevelBox(PanelWindow -> RPort,8,13,PanelWindow -> Width - 16,PanelWindow -> Height - (13 + 5),
  234.             GT_VisualInfo,    VisualInfo,
  235.             GTBB_Recessed,    TRUE,
  236.         TAG_DONE);
  237.  
  238.         while(!Terminated)
  239.         {
  240.             WaitPort(PanelWindow -> UserPort);
  241.  
  242.             while(!Terminated && (Massage = (struct IntuiMessage *)GetMsg(PanelWindow -> UserPort)))
  243.             {
  244.                 Class    = Massage -> Class;
  245.                 Code    = Massage -> Code;
  246.  
  247.                 ReplyMsg(Massage);
  248.  
  249.                 if(Class == IDCMP_CLOSEWINDOW)
  250.                     Terminated = TRUE;
  251.  
  252.                 if(Class == IDCMP_VANILLAKEY)
  253.                 {
  254.                     switch(Code)
  255.                     {
  256.                         case '\033':    Terminated = TRUE;
  257.                                 break;
  258.  
  259.                         case '\r':    Terminated = TRUE;
  260.  
  261.                                 if(Count)
  262.                                     Result = TRUE;
  263.  
  264.                                 break;
  265.  
  266.                         case '\b':    if(Count)
  267.                                     Buffer[Count--] = 0;
  268.                                 else
  269.                                     DisplayBeep(PanelWindow -> WScreen);
  270.  
  271.                                 break;
  272.  
  273.                         default:    if((Code >= ' ' && Code < 127) || Code >= 160)
  274.                                 {
  275.                                     Buffer[Count++] = Code;
  276.  
  277.                                     if(Count == 20)
  278.                                         Terminated = Result = TRUE;
  279.                                 }
  280.                                 else
  281.                                     DisplayBeep(PanelWindow -> WScreen);
  282.  
  283.                                 break;
  284.                     }
  285.                 }
  286.             }
  287.         }
  288.  
  289.         CloseWindow(PanelWindow);
  290.     }
  291.  
  292.     return(Result);
  293. }
  294.